/**
 * Created by duwei on 2018/6/4.
 */
var RecordCellItem = Item.extend({
    _ccsFileName: res.RecordCellUnit_json,
    _itemHeight:151,
    _itemaddHeight:50,
    playerCount: 4,
    /**
     * 点击事件
     * @private
     */
    _initClickListeners: function () {
        this._super();
        this._clickListeners = {
            '_btn_xiangqing': '_clickXiangQing'
        };
    },
    /**
     * 点击详情
     * @private
     */
    _clickXiangQing: function () {
        cc.log(this.data);
        showWindow('RecordDetailWindow', this.data);
    },
    /**
     * 设置数据
     * @param index
     * @param data
     * @param clubid
     */
    refreshData: function (index, data, clubid) {
        var self = this;
        self.index = index;
        self.data = data;
        self.clubid = clubid;
        var length = 0;
        for (var k = 1; k <= 9; k++) {
            if (data["uid" + k]) {
                length++;
            }
        }
        self.playerCount = length;

        var root = self._nodeList["_root"];
        if(!root){
            return;
        }
        var timeArr = timestamp2time(data['create_time'] || 0).split(' ');
        root["_time1"].setString(timeArr[0]);
        root["_time2"].setString(timeArr[1]);
        root["_lb_roomid"].setString(data['room_id']);
        if(data['wanfa']){
            root["_lb_wanfa"].setString(self.handleWanfaDesp(data['wanfa']));
        }else if(data['optionstring']){
            var wanfaArr = JSON.parse(data['optionstring']);
            root["_lb_wanfa"].setString(wanfaArr['wanfadesc'] || wanfaArr['desp']);
        }else{
            root["_lb_wanfa"].setString('');
        }
        var row = length > 0 ? Math.floor((length - 1) / 4) : 0;
        root["_itembg"].setContentSize(cc.size(self._nodeList["_itembg"].getContentSize().width, self._itemHeight + row * self._itemaddHeight));
        //self._nodeList['_btn_xiangqing'].setPositionY(self._itemaddHeight/2 + row * self._itemaddHeight/2);
        root.setPositionY(row * 72);
        var infoNode = root["_infoNode"];

        //服务器重启后总结果无数据
        if (data['total_score_arr'] == undefined || data['total_score_arr'] == null ||
            (data['total_score_arr'] && data['total_score_arr'].length == 0)){
            data['total_score_arr'] = [];
            var tmpScoreArr = [];
            for (var i = 0; i < self.playerCount; i++){
                tmpScoreArr[i] = 0;
                for (var j = 0; j < data['result_arr'].length; j++){
                    tmpScoreArr[i] += data['result_arr'][j][i + 1];
                }
            }
            data['total_score_arr'] = tmpScoreArr;
        }
        
        for (var i = 1; i <= self.playerCount; i++) {
            (function (i) {
                var info = infoNode["_info" + i];
                if (!info) {
                    info = duplicateNode(infoNode["_info0"]);
                    var row = Math.floor((i - 1) / 4);
                    var col = (i - 1) % 4 + 1;
                    info.setPosition(cc.p(infoNode["_info" + col].getPositionX(), infoNode["_info" + col].getPositionY() - row * 72));
                    infoNode["_info" + i] = info;
                    info.setVisible(true);
                    infoNode.addChild(info);
                }
                if (data["nickname" + i]) {
                    info.setVisible(true);
                    var head = info["head"];
                    var fangzhu = info["fangzhu"];
                    var lb_name = info["lb_name"];
                    var lb_score = info["lb_score"];

                    lb_name.setString(ellipsisStr(data["nickname" + i], 7) + "");
                    if (data['result_arr'][i - 1] >= 0) {
                        lb_score.setTextColor(cc.color("#bc6f00"));
                    } else {
                        lb_score.setTextColor(cc.color("#43873A"));
                    }
                    lb_score.setString(data['total_score_arr'][i - 1]['total_score']);

                    //头像
                    var heads = data["heads"];
                    if (!_.isArray(heads)) {
                        heads = data["heads"].split(',');
                        for (var k = 0; k < heads.length; k++) {
                            heads[k] = heads[k].replace(/'/g, "");
                        }
                    }


                    var url = decodeURIComponent(heads[i - 1]);
                    if (url == undefined || (url.length == 0)) url = res.defaultHead;
                    loadCircleConorToSprite(url, head, head.getContentSize().width / 2);
                }
            })(i)
        }
        for (; i <= infoNode.children.length; i++) {
            var info = infoNode["_info" + i];
            if (info) {
                info.setVisible(false);
            }
        }

        //是不是自己赢了
        var myscore = 0;
        var isMy = false;
        var fangzhuIndex = 0;
        for (var i = 1; i <= self.playerCount; i++) {
            if (data["uid" + i] == gameData.uid) {
                isMy = true;
                if (data['result_arr'][0][i])
                    myscore = data['result_arr'][0][i];
                break;
            }
        }
        for (var i = 1; i <= self.playerCount; i++) {
            //谁是房主
            if (data["uid" + i] && data.owner_uid == data["uid" + i]) {
                fangzhuIndex = i;
                break;
            }
        }
        for (var i = 1; i <= self.playerCount; ++i) {
            infoNode["_info" + i]["fangzhu"].setVisible((i == fangzhuIndex));
        }


        self._nodeList["_ying"].setVisible(false);
        self._nodeList["_shu"].setVisible(false);
        self._nodeList["_ping"].setVisible(false);
        if (self.clubid) {
            if (myscore > 0) {
                self._nodeList["_ying"].setVisible(true);
            } else if (myscore < 0) {
                self._nodeList["_shu"].setVisible(true);
            } else {
                self._nodeList["_ping"].setVisible(isMy);
            }
        } else {
            if (fangzhuIndex == 0) {

            } else if (myscore > 0) {
                self._nodeList["_ying"].setVisible(true);
            } else if (myscore < 0) {
                self._nodeList["_shu"].setVisible(true);
            } else {
                self._nodeList["_ping"].setVisible(true);
            }
        }
        TouchUtil.setOnclickListener(self._nodeList['_btn_xiangqing'], function () {
            if (self.data) {
                self._clickXiangQing();
            }
        });
    },
    handleWanfaDesp: function (wanfaDesp){
        var removedStrArr =['群主支付', '房主支付', 'AA支付'];
        var retVal = wanfaDesp;
        var retValArr = retVal.split(',');
        var locationArr = [];
        for (var i = 0; i < retValArr.length; i++){
            retValArr[i] = retValArr[i].trim();
            if (removedStrArr.indexOf(retValArr[i]) > -1) locationArr.push(i);
        }

        for (var i = locationArr.length - 1; i > -1; i--){
            retValArr.splice(locationArr[i], 1);
        }

        retVal = retValArr.join(',');

        return retVal;
    },
});